﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _00365.Biz
{

    public class Login
    {

        public static bool Authenticate(string username, string password, out Data.Login AuthUser, out List<Data.Feature> Permissions)
        {
            Data.Login user = (from i in DBInstance.Models.Logins
                               where i.Username.ToUpper().Equals(username.ToUpper()) &&
                                     i.Password.Equals(password) &&
                                     i.Active.Equals(true)
                               select i).FirstOrDefault();
            if (user != null)
            {
                user.UserGroups.Load();
                if (user.UserGroups.Count > 0)
                {
                    AuthUser = user;
                    Permissions = GetPermissions(user);
                    return true;
                }
            }
            AuthUser = null;
            Permissions = null;
            return false;
        }

        public static List<Data.Feature> GetPermissions(Data.Login user)
        {
            if (user != null)
            {
                List<Data.Feature> perms = new List<Data.Feature>();

                var items = from i in user.UserGroups
                            from j in i.Permission
                            where i.Active == true && j.Active == true
                            select j.Feature;

                perms.AddRange(items);

                return perms;
            }
            return null;
        }

        public static List<Data.Login> GetAll()
        {
            List<Data.Login> list = (from i in DBInstance.Models.Logins
                                     orderby i.Username
                                     select i).ToList<Data.Login>();
            return list;
        }

        public static List<Data.Login> GetUsersButFrom(Data.UserGroup Group)
        {
            return DBInstance.Models.Logins.ToList().Except(Group.Logins.ToList()).ToList();            
        }
        
        public static Data.Login Get(Guid Id)
        {
            Data.Login login = (from i in DBInstance.Models.Logins
                                    where i.Id.Equals(Id)
                                    select i).FirstOrDefault();
            if (login != null) return login;
            return null;
        }

        public static void Add(Data.Login newUser)
        {
            newUser.Password = Sys.Encrypt(newUser.Password);
            DBInstance.Models.AddToLogins(newUser);
            DBInstance.Models.SaveChanges();
        }

        internal static void Update(Data.Login newUser)
        {
            Data.Login user = (from i in DBInstance.Models.Logins
                                 where i.Id.Equals(newUser.Id)
                                 select i).FirstOrDefault();
            if (user != null)
            {
                user.FullName = newUser.FullName;
                user.Username = newUser.Username;
                //user.Password = Sys.Encrypt(newUser.Password);
                user.Active = newUser.Active;
                DBInstance.Models.SaveChanges();
            }
        }

        public static void Remove(Guid Id)
        {
            Data.Login user = (from i in DBInstance.Models.Logins
                                    where i.Id.Equals(Id)
                                    select i).FirstOrDefault();
            if (user != null)
            {
                DBInstance.Models.Logins.DeleteObject(user);
                DBInstance.Models.SaveChanges();
            }
        }

        public static void ChangePassword(Data.Login userData, string newPassword)
        {
            Data.Login user = (from i in DBInstance.Models.Logins
                               where i.Id.Equals(userData.Id)
                               select i).FirstOrDefault();
            if (user != null)
            {
                user.Password = Sys.Encrypt(newPassword);
                DBInstance.Models.SaveChanges();
            }
        }
    }
}
